// pages/leaseDetail/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    imgList: [],

    popupFlag: false, // 租赁申请弹窗
    productFlag: false, // 级联选择
    productList: [],

    fieldNames: {
      text: 'label',
      value: 'id'
    },

    // 租赁申请弹窗
    lease: {
      productName: '',
      productTypeName: '',
      productId: '',
      productTypeId: '',

      weight: '',
      contactName: '',
      contactPhone: '',
      reason: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const eventChannel = this.getOpenerEventChannel()

    eventChannel.on('info', (res) => {
      const info = JSON.parse(res.data)
      this.setData({ info })

      const imgList = JSON.parse(info.attachment) 

      if (imgList.length > 0) {
        this.setData({ imgList })
      }
    })	

    // 请求级联请求的数据
    wx.$req({
      url: '/wms/itemType/treeselect',
      loadingText: '加载中...'
    }).then(res => {
      this.setData({ 
        productList: res.data.data,
      })
    })
  },

  onChange(e) {
    const field = e.target.dataset.field
    const lease = this.data.lease
    lease[field] = e.detail
  },

  // 图片预览
  preview (e) {
    const urls = this.data.imgList.map(item => item.url)

    wx.previewImage({
      urls,
      current: e.currentTarget.dataset.url,
    })
  },

  // 关闭弹窗，同时清空内容
  cancel () {
    // 清空lease中的值
    const lease = this.data.lease
    for(let key in lease) {
      lease[key] = ''
    }

    this.setData({ 
      popupFlag: false,
      lease
    })
  },

  // 租赁 确认
  confirm () {
    // 校验
    if (!this.data.lease.productTypeId) return wx.showToast({  title: '请选择储藏产品', icon: 'none'  })
    if (!this.data.lease.weight) return wx.showToast({  title: '请输入重量', icon: 'none'  })

    if (this.data.lease.weight <= 0 || this.data.lease.weight > 9999999) {
      return wx.showToast({  title: '请输入合理的重量', icon: 'none'  })
    }

    if (!this.data.lease.contactName) return wx.showToast({  title: '请输入联系人', icon: 'none'  })

    if (!this.data.lease.contactPhone) return wx.showToast({  title: '请输入联系人手机号', icon: 'none'  })
    
    const phoneReg = /^1[3-9]\d{9}$/
    if (!phoneReg.test(this.data.lease.contactPhone)) {
      return wx.showToast({  title: '请输入正确的手机号', icon: 'none'  })
    }

    wx.showLoading({ title: '提交中...', })
    wx.$req({
      url: '/wms/coldStorage/addLease',
      method: 'post',
      data: {
        ...this.data.lease,
        coldStorageId: this.data.info.id
      }
    }).then(res => {
      wx.hideLoading()
      wx.showToast({  title: '提交成功' })
      this.cancel()
    }).finally(() => { wx.hideLoading() })
  },

  // 打开租赁申请弹窗
  leaseApplication () {
    this.setData({ popupFlag: true})
  },

  // 打开级联选择弹窗
  openProduct () {
    this.setData({  productFlag: true })
  },

  // 级联选择完毕
  onFinish (e) {
    const result = e.detail.selectedOptions

    let productName = result[0].label,
    productTypeName = result[1].label,
    productId = result[0].id,
    productTypeId = result[1].id

    const lease = this.data.lease
    Object.assign(lease, { productName, productTypeName, productId, productTypeId })

    this.setData({ lease }, () => {
      this.onClose()
    })
  },

  // 关闭级联选择
  onClose () {
    this.setData({ productFlag: false })
  }

})


